package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;


    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Override
    public ApartmentDetailVo listApartmentDetailVoByApartmentId(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 根据公寓id查询公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        // 查询最小租金
        BigDecimal minRent = apartmentInfoMapper.selectMinRent(id);
        // 查询图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphInfoListByApartmentId(id);
        // 封装图片列表
        List<GraphVo> graphVoList = graphInfoList.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        // 查询配套列表
        List<FacilityInfo> facilityInfoList= apartmentFacilityMapper.selectFacilityInfoListByApartmentId(id);
        // 查询标签列表
        List<LabelInfo> labelInfoList = apartmentLabelMapper.selectLabelInfoListByApartmentId(id);

        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        return apartmentDetailVo;
    }
}




